#include "editform.h"
#include "ui_editform.h"
#include <QDebug>

EditForm::EditForm(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::EditForm)
{
    ui->setupUi(this);
    ui->groupBox_subtitle_position->setVisible(false);
    ui->groupBox_subtitle_interval->setVisible(false);
    ui->groupBox_front_text->setVisible(false);
    ui->groupBox_front_setting->setVisible(false);
    ui->groupBox_box_start->setVisible(false);
    ui->groupBox_box_end->setVisible(false);
    ui->groupBox_box_type->setVisible(false);
    ui->groupBox_box_interval->setVisible(false);
}

EditForm::~EditForm()
{
    delete ui;
}

void EditForm::getFrame(Mat frame,double width,double height,int frame_now)
{
    this->frame = frame;
    this->width = width;
    this->height = height;
    ui->lineEdit_subtitle_start->setText(QString::number(frame_now));
    ui->lineEdit_box_start->setText(QString::number(frame_now));
}

void EditForm::showFrame()
{
    if(!frame.empty())
    {
        QImage img = QImage((const unsigned char*)(frame.data),
                            frame.cols,
                            frame.rows,
                            QImage::Format_RGB888
                            ).rgbSwapped();
        ui->label_frame->resize(QSize(width,height));
        ui->label_frame->setScaledContents(true);
        QSize qs = ui->label_frame->rect().size();
        ui->label_frame->setPixmap(QPixmap::fromImage(img).scaled(qs));
    }

}

list<SubtitleInfo> EditForm::getSubtitle()
{
    qDebug()<<"run1";
    return l_subtitle;
}

list<BoxInfo> EditForm::getBox()
{
    qDebug()<<"run2";
    return l_box;
}

void EditForm::mousePressEvent(QMouseEvent *event)
{
    PositionInfo pos;
    QPoint position =event->globalPos();
    position = ui->label_frame->mapFromGlobal(position);
    if(event->button() == Qt::LeftButton)
    {
        pos.x1 = position.x();
        pos.y1 = position.y();
        ui->lineEdit_x->setText(QString::number(pos.x1));
        ui->lineEdit_y->setText(QString::number(pos.y1));
        ui->lineEdit_x1->setText(QString::number(pos.x1));
        ui->lineEdit_y1->setText(QString::number(pos.y1));
    }
    if(event->button() == Qt::RightButton)
    {
        pos.x2 = position.x();
        pos.y2 = position.y();
        ui->lineEdit_x2->setText(QString::number(pos.x2));
        ui->lineEdit_y2->setText(QString::number(pos.y2));
    }
}

void EditForm::closeEvent(QCloseEvent *event)
{
    event = event;
    emit editClose(0);
}

void EditForm::on_pushButton_subtitle_clicked()
{
    ui->groupBox_subtitle_position->setVisible(true);
    ui->groupBox_subtitle_interval->setVisible(true);
    ui->groupBox_front_text->setVisible(true);
    ui->groupBox_front_setting->setVisible(true);
    ui->lineEdit_subtitle_end->setFocus();
}

void EditForm::on_pushButton_box_clicked()
{
    ui->groupBox_box_start->setVisible(true);
    ui->groupBox_box_end->setVisible(true);
    ui->groupBox_box_type->setVisible(true);
    ui->groupBox_box_interval->setVisible(true);
    ui->lineEdit_box_end->setFocus();
}

void EditForm::on_pushButton_subtitle_sure_clicked()
{
    ui->groupBox_subtitle_position->setVisible(false);
    ui->groupBox_subtitle_interval->setVisible(false);
    ui->groupBox_front_text->setVisible(false);
    ui->groupBox_front_setting->setVisible(false);
    ui->label_frame->setMaximumSize(width,height);

    SubtitleInfo sub_info;
    BGRInfo bgr;
    //选择颜色
    switch (ui->comboBox_front_color->currentIndex()) {
    case 0:
        bgr.b = 0;
        bgr.g = 0;
        bgr.r = 255;
        break;
    case 1:
        bgr.b = 0;
        bgr.g = 255;
        bgr.r = 255;
        break;
    case 2:
        bgr.b = 0;
        bgr.g = 255;
        bgr.r = 0;
        break;
    case 3:
        bgr.b = 255;
        bgr.g = 191;
        bgr.r = 0;
        break;
    case 4:
        bgr.b = 255;
        bgr.g = 0;
        bgr.r = 0;
        break;
    case 5:
        bgr.b = 255;
        bgr.g = 48;
        bgr.r = 155;
        break;
    case 6:
        bgr.b = 255;
        bgr.g = 250;
        bgr.r = 250;
        break;
    case 7:
        bgr.b = 130;
        bgr.g = 130;
        bgr.r = 130;
        break;
    case 8:
        bgr.b = 28;
        bgr.g = 28;
        bgr.r = 28;
        break;
    default:
        break;
    }

    if(ui->radioButton_Chinese->isChecked())
    {
        sub_info.front_Chinese_sign = 1;
        QString str = ui->lineEdit_text->text();
        const wchar_t *theRealSub = reinterpret_cast<const wchar_t *>(str.utf16());
        switch (ui->comboBox_front_type->currentIndex()) {
        case 0:
            cvtext = new CvxText("C:\\Windows\\Fonts\\AdobeSongStd-Light.otf"); //宋体
            break;
        case 1:
            cvtext = new CvxText("C:\\Windows\\Fonts\\AdobeFangsongStd-Regular.otf"); //仿宋
            break;
        case 2:
            cvtext = new CvxText("C:\\Windows\\Fonts\\AdobeKaitiStd-Regular.otf"); //楷体
            break;
        case 3:
            cvtext = new CvxText("C:\\Windows\\Fonts\\msyh.ttc"); //微软雅黑
            break;
        case 4:
            cvtext = new CvxText("C:\\Windows\\Fonts\\AdobeHeitiStd-Regular.otf"); //黑体
            break;
        default:
            break;
        }
        Scalar size = ui->comboBox_front_size->currentText().toInt();
        cvtext->setFont(NULL,&size,NULL,NULL);
        cvtext->putText(frame,theRealSub,Point(ui->lineEdit_x->text().toInt(),
                                              ui->lineEdit_y->text().toInt()),
                       Scalar(bgr.b,bgr.g,bgr.r));
        showFrame();

        delete cvtext;
        cvtext = NULL;
    }
    else
    {
        cv::putText(frame, ui->lineEdit_text->text().toStdString(),
                    Point(ui->lineEdit_x->text().toInt(),
                          ui->lineEdit_y->text().toInt()),
                    FONT_HERSHEY_TRIPLEX, 0.8, Scalar(bgr.b,bgr.g,bgr.r),
                    0.6, CV_AA);
        showFrame();
    }

    sub_info.front_x = ui->lineEdit_x->text().toInt();
    sub_info.front_y = ui->lineEdit_y->text().toInt();

    sub_info.front_start = ui->lineEdit_subtitle_start->text().toInt();
    sub_info.front_end = ui->lineEdit_subtitle_end->text().toInt();

    sub_info.front_text = ui->lineEdit_text->text().toStdString();
    sub_info.front_size = ui->comboBox_front_size->currentText().toInt();
    sub_info.front_type = ui->comboBox_front_type->currentIndex();
    sub_info.front_color_b = bgr.b;
    sub_info.front_color_g = bgr.g;
    sub_info.front_color_r = bgr.r;
    l_subtitle.push_back(sub_info);

}

void EditForm::on_pushButton_box_sure_clicked()
{
    ui->groupBox_box_start->setVisible(false);
    ui->groupBox_box_end->setVisible(false);
    ui->groupBox_box_type->setVisible(false);
    ui->groupBox_box_interval->setVisible(false);
    ui->label_frame->setMaximumSize(width,height);

    BGRInfo bgr;
    //选择颜色
    switch (ui->comboBox_line_color->currentIndex()) {
    case 0:
        bgr.b = 0;
        bgr.g = 0;
        bgr.r = 255;
        break;
    case 1:
        bgr.b = 0;
        bgr.g = 255;
        bgr.r = 255;
        break;
    case 2:
        bgr.b = 0;
        bgr.g = 255;
        bgr.r = 0;
        break;
    case 3:
        bgr.b = 255;
        bgr.g = 191;
        bgr.r = 0;
        break;
    case 4:
        bgr.b = 255;
        bgr.g = 0;
        bgr.r = 0;
        break;
    case 5:
        bgr.b = 255;
        bgr.g = 48;
        bgr.r = 155;
        break;
    case 6:
        bgr.b = 255;
        bgr.g = 250;
        bgr.r = 250;
        break;
    case 7:
        bgr.b = 130;
        bgr.g = 130;
        bgr.r = 130;
        break;
    case 8:
        bgr.b = 28;
        bgr.g = 28;
        bgr.r = 28;
        break;
    default:
        break;
    }

    //画直线
    if(ui->radioButton_line->isChecked())
    {
        line(frame,
             Point(ui->lineEdit_x1->text().toInt(),
                   ui->lineEdit_y1->text().toInt()),
             Point(ui->lineEdit_x2->text().toInt(),
                   ui->lineEdit_y2->text().toInt()),
             Scalar(bgr.b,bgr.g,bgr.r),
             ui->comboBox_line_size->currentText().toInt(),
             LINE_4);
        showFrame();
        box_type = 3;
    }

    //画圆
    if(ui->radioButton_circle->isChecked())
    {
        circle(frame,
               Point(ui->lineEdit_x1->text().toInt() +
                     (ui->lineEdit_x2->text().toInt() -
                      ui->lineEdit_x1->text().toInt()) / 2,
                     ui->lineEdit_y1->text().toInt() +
                     (ui->lineEdit_y2->text().toInt() -
                      ui->lineEdit_y1->text().toInt()) / 2),
               sqrt((ui->lineEdit_x2->text().toInt() -
                     ui->lineEdit_x1->text().toInt()) *
                    (ui->lineEdit_x2->text().toInt() -
                     ui->lineEdit_x1->text().toInt()) +
                    (ui->lineEdit_y2->text().toInt() -
                       ui->lineEdit_y1->text().toInt()) *
                    (ui->lineEdit_y2->text().toInt() -
                     ui->lineEdit_y1->text().toInt())) / 2,
               Scalar(bgr.b,bgr.g,bgr.r),
               ui->comboBox_line_size->currentText().toInt(),
               LINE_4);
        showFrame();
        box_type = 2;
    }

    //画椭圆
    if(ui->radioButton_ellipse->isChecked())
    {
        ellipse(frame,
                Point(ui->lineEdit_x1->text().toInt() +
                      (ui->lineEdit_x2->text().toInt() -
                       ui->lineEdit_x1->text().toInt()) / 2,
                      ui->lineEdit_y1->text().toInt() +
                      (ui->lineEdit_y2->text().toInt() -
                       ui->lineEdit_y1->text().toInt()) / 2),
                Size((ui->lineEdit_x2->text().toInt() -
                      ui->lineEdit_x1->text().toInt()) / 2,
                     (ui->lineEdit_y2->text().toInt() -
                      ui->lineEdit_y1->text().toInt()) / 2),
                0,
                0,
                360,
                Scalar(bgr.b,bgr.g,bgr.r),
                ui->comboBox_line_size->currentText().toInt(),
                LINE_4);
        showFrame();
        box_type = 1;
    }

    //画矩形
    if(ui->radioButton_rectangular->isChecked())
    {
        rectangle(frame,Rect(ui->lineEdit_x1->text().toInt(),
                             ui->lineEdit_y1->text().toInt(),
                       (ui->lineEdit_x2->text().toInt() -
                        ui->lineEdit_x1->text().toInt()),
                       (ui->lineEdit_y2->text().toInt() -
                        ui->lineEdit_y1->text().toInt())),
                  Scalar(bgr.b,bgr.g,bgr.r),
                  ui->comboBox_line_size->currentText().toInt(),
                  LINE_4);
        showFrame();
        box_type = 0;
    }
    BoxInfo box_info;
    box_info.box_x1 = ui->lineEdit_x1->text().toInt();
    box_info.box_x2 = ui->lineEdit_x2->text().toInt();
    box_info.box_y1 = ui->lineEdit_y1->text().toInt();
    box_info.box_y2 = ui->lineEdit_y2->text().toInt();

    box_info.box_start = ui->lineEdit_box_start->text().toInt();
    box_info.box_end = ui->lineEdit_box_end->text().toInt();

    box_info.box_type =box_type;
    box_info.box_line = ui->comboBox_line_size->currentText().toInt();
    box_info.box_color_b = bgr.b;
    box_info.box_color_g = bgr.g;
    box_info.box_color_r = bgr.r;
    l_box.push_back(box_info);
}

void EditForm::on_pushButton_finish_clicked()
{
    this->close();
}
